* This is the do file to create "Table 2. Internal Validity in the Presence of Attrition"
set seed 123

use "$path_data/temp/student_unbalance", clear

gen female = 1 if student_gender == 0
gen grade_2 = 1 if grade == 2
recode female grade_2(.=0)

gen tracked = 1 - attrition

/// Standardization
egen DT_score_pre_mean = mean(DT_score_pre)
egen DT_score_pre_sd = sd(DT_score_pre)
gen DT_score_pre_std = (DT_score_pre-DT_score_pre_mean)/DT_score_pre_sd
drop DT_score_pre_mean DT_score_pre_sd 

egen cpcs_pre_mean = mean(cpcs_pre)
egen cpcs_pre_sd = sd(cpcs_pre)
gen cpcs_pre_std = (cpcs_pre-cpcs_pre_mean)/cpcs_pre_sd
drop cpcs_pre_mean cpcs_pre_sd 

egen rosen_pre_mean = mean(rosen_pre)
egen rosen_pre_sd = sd(rosen_pre)
gen rosen_pre_std = (rosen_pre-rosen_pre_mean)/rosen_pre_sd
drop rosen_pre_mean rosen_pre_sd 


replace school_no = 999 if school_no ==.

/// Group indicators
gen group = 0 if treatment == 1 & attrition == 0
replace group = 1 if treatment == 0 & attrition == 0
replace group = 2 if treatment == 1 & attrition == 1
replace group = 3 if treatment == 0 & attrition == 1


/// Mean difference
foreach j in mean sd {
tabstat DT_score_pre_std rosen_pre_std cpcs_pre_std female grade_2 if group == 0, stat(`j') save
matrix list r(StatTotal)
matrix `j'_TR = r(StatTotal)

tabstat DT_score_pre_std rosen_pre_std cpcs_pre_std female grade_2 if group == 1, stat(`j') save
matrix list r(StatTotal)
matrix `j'_CR = r(StatTotal)

tabstat DT_score_pre_std rosen_pre_std cpcs_pre_std female grade_2 if group == 2, stat(`j') save
matrix list r(StatTotal)
matrix `j'_TA = r(StatTotal)

tabstat DT_score_pre_std rosen_pre_std cpcs_pre_std female grade_2 if group == 3, stat(`j') save
matrix list r(StatTotal)
matrix `j'_CA = r(StatTotal)
}

/// Internal Validity for the Respondents
foreach outcome in DT_score_pre_std rosen_pre_std cpcs_pre_std female grade_2 {
regress `outcome' i.group
test (0.group = 1.group) (2.group = 3.group)
scalar IValR_p_`outcome' = r(p)
}

/// Internal Validity for the Population

foreach outcome in DT_score_pre_std rosen_pre_std cpcs_pre_std female grade_2 {
regress `outcome' i.group
test (0.group = 1.group) (1.group = 2.group) (2.group = 3.group)
scalar IValP_p_`outcome' = r(p)
}

/// Make Table
tempname hh2
file open `hh2' using "$path_output/ghanem_attrition_test.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Internal validity in the presence of attrition for the self-learning at the right level program}" _newline
file write `hh2' "\label{tab:ghanem_attrition}" _newline
file write `hh2' "\scalebox{1}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lcccccc}\toprule" _newline

file write `hh2' "   & \multicolumn{4}{c}{Mean Baseline Outcome by Group} & \multicolumn{2}{c}{p-value} \\ " _newline
file write `hh2' " \cmidrule(lr){2-5} \cmidrule(lr){6-7} "
file write `hh2' " Dependent Variable & TR & CR & TA & CA & IVal-R & IVal-P   \\\midrule\midrule" _newline
file write `hh2' " DT score^{a} & " %04.3f (mean_TR[1,1]) " & " %04.3f (mean_CR[1,1]) " & " %04.3f (mean_TA[1,1]) " & " %04.3f (mean_CA[1,1]) " & " %04.3f (IValR_p_DT_score_pre_std) " & " %04.3f (IValP_p_DT_score_pre_std) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_TR[1,1]) " ] & [ " %04.3f (sd_CR[1,1]) " ] & [ " %04.3f (sd_TA[1,1]) " ] &  [ " %04.3f (sd_CA[1,1]) " ] & &    \\ " _newline

file write `hh2' " RSES^{a} & " %04.3f (mean_TR[1,2]) " & " %04.3f (mean_CR[1,2]) " & " %04.3f (mean_TA[1,2]) " & " %04.3f (mean_CA[1,2]) " & " %04.3f (IValR_p_rosen_pre_std) " & " %04.3f (IValP_p_rosen_pre_std) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_TR[1,2]) " ] & [ " %04.3f (sd_CR[1,2]) " ] & [ " %04.3f (sd_TA[1,2]) " ] &  [ " %04.3f (sd_CA[1,2]) " ] & &    \\ " _newline

file write `hh2' " CPCS^{a} & " %04.3f (mean_TR[1,3]) " & " %04.3f (mean_CR[1,3]) " & " %04.3f (mean_TA[1,3]) " & " %04.3f (mean_CA[1,3]) " & " %04.3f (IValR_p_cpcs_pre_std) " & " %04.3f (IValP_p_cpcs_pre_std) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_TR[1,3]) " ] & [ " %04.3f (sd_CR[1,3]) " ] & [ " %04.3f (sd_TA[1,3]) " ] &  [ " %04.3f (sd_CA[1,3]) " ] & &    \\ " _newline

file write `hh2' " Female & " %04.3f (mean_TR[1,4]) " & " %04.3f (mean_CR[1,4]) " & " %04.3f (mean_TA[1,4]) " & " %04.3f (mean_CA[1,4]) " & " %04.3f (IValR_p_female) " & " %04.3f (IValP_p_female) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_TR[1,4]) " ] & [ " %04.3f (sd_CR[1,4]) " ] & [ " %04.3f (sd_TA[1,4]) " ] &  [ " %04.3f (sd_CA[1,4]) " ] & &    \\ " _newline

file write `hh2' " Grade 3 & " %04.3f (mean_TR[1,5]) " & " %04.3f (mean_CR[1,5]) " & " %04.3f (mean_TA[1,5]) " & " %04.3f (mean_CA[1,5]) " & " %04.3f (IValR_p_grade_2) " & " %04.3f (IValP_p_grade_2) " \\ " _newline
file write `hh2' "    				 & [ " %04.3f (sd_TR[1,5]) " ] & [ " %04.3f (sd_CR[1,5]) " ] & [ " %04.3f (sd_TA[1,5]) " ] &  [ " %04.3f (sd_CA[1,5]) " ] & &    \\ " _newline

file write `hh2' " \\ "_newline



file write `hh2' "\midrule" _newline
file write `hh2' "\end{tabular}" _newline
file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item (a) Variables are standardized using the average and variance of the whole baseline sample in the March 2016 survey. " _newline
file write `hh2' "\item (b) Standard deviations are reported in square brackets." _newline
file write `hh2' "\item (c) The mean baseline outcomes correspond to the groups of treatment respondents (TR), control respondents (CR), treatment attritors (TA), and control attritors (CA)." _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\label{tab:addlabel}%" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'
